# AUTHOR: DING
# -*- codeing = utf-8 -*-
# @Time: 2024/2/3 15:35
# @Author: 86139
# @Site: 
# @File: 07-transform.py
# @Software: PyCharm

from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
import cv2 as cv
from PIL import Image

writer = SummaryWriter("logs")
img_path = "../test_data/train/ants_image/0013035.jpg"
img = Image.open(img_path)

# img = cv.imread(img_path)
# # 1.工具箱 transforms使用
trans_totensor = transforms.ToTensor()  # 选择一个class创建
tensor_img = trans_totensor(img)  # Ctrl+P查看需要的参数，调用call,
writer.add_image("toTensor", tensor_img)

# 归一化
trans_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_norm = trans_norm(tensor_img)
writer.add_image("Norm", img_norm)

# resize
h = int(len(tensor_img[0]) / 2)
w = int(len(tensor_img[0][0]) / 2)
trans_resize = transforms.Resize((h, w))

img_resize = trans_resize(img)  # PIl -> resize ->resize PIl

img_resize = trans_totensor(img_resize)  # resize PIl -> tensor ->resize tensor
writer.add_image("Resize", img_resize)

# Compose resize -2
trans_resize_2 = transforms.Resize(512)
trans_compose = transforms.Compose([trans_resize_2, trans_totensor])  # Compose汇总resize和totensor两个步骤
img_resize_2 = trans_compose(img)
writer.add_image("Resize_2", img_resize_2)

writer.close()
# print(tensor_img)
# 2.Tensor的数据类型
